package cn.jsoup;

import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.File;
import java.io.IOException;
import java.util.List;

//使用Xpath查询html中的元素
public class XpathDemo {
    public static void main(String[] args) throws IOException, XpathSyntaxErrorException {
        String path = XpathDemo.class.getClassLoader().getResource("student.xml").getPath();
        //获取html文件的路径
        Document  document= Jsoup.parse(new File(path), "UTF-8");
        //将html文件进行解析
        JXDocument  jd=new JXDocument(document);
        //创建JXDocument对象并将html对象作为参数传递进去
        List<JXNode> jxNodes = jd.selN("//student");
        //通过标签直接查询
        for (JXNode jxNode : jxNodes) {
            System.out.println(jxNode);
        }
        List<JXNode> jxNodes1 = jd.selN("//student/name");
        //通过标签直接查询
        for (JXNode jxNode : jxNodes1) {
            Element element = jxNode.getElement();
            String textname = element.text();
            System.out.println(textname);
        }
        List<JXNode> jxNodes2 = jd.selN("//student/name[@id]");
        //通过id查询标签
        List<JXNode> jxNodes3 = jd.selN("//student/name[@id='itcast']");
          //通过id属性值查询
    }
}
